package models

import (
	"SSO/tools"
)

type Admin struct {
	Id       int    `json:"id"`
	UserId string `json:"user_id"`
}

type Admins struct {}

func(Admins) GetByUserId(userId int) (*Admin, error){
	admins := make([]Admin, 0)
	r := tools.GetMysqlDB().Where("user_id = ?", userId).Find(&admins)
	if r.Error != nil {
		return nil, r.Error
	}

	if len(admins) == 0 {
		return nil, nil
	}

	return &admins[0], nil
}

func (this Admins) IsAdmin(username string)(r bool, err error){
	user, err := Users{}.GetByUsername(username)
	if err != nil {
		return
	}

	admin, err := this.GetByUserId(user.Id)

	if err != nil {
		return
	}

	if admin != nil {
		r = true
	}
	return
}